package user_information;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

import Tool.Properties_tool;

public class Phone_modify_msg_send extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String phone = request.getParameter("new_phone");
		
		//获取真实路径
	    Properties_tool pp = new Properties_tool(request.getSession().getServletContext().getRealPath("/WEB-INF/classes/user_information/Msg_send_set.properties"));
	    
		//获取各项参数
		String appkey = pp.getvalue("appkey");         
		String url = pp.getvalue("url");
		String secret = pp.getvalue("secret");
		String autograph = pp.getvalue("autograph");
		String Template_id = pp.getvalue("Template_id");
		String product_name = pp.getvalue("product_name");
	    
		int verification_code;
		verification_code = (int)(Math.random()*90000)+10000;      //产生验证码
		
		TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setExtend("0");     //设置返回状态码
		req.setSmsType("normal");
		req.setSmsFreeSignName(autograph);                        //设置签名
		req.setSmsParam("{'code':'"+verification_code+"','product':'"+product_name+"'}");
		req.setRecNum(phone);
		req.setSmsTemplateCode(Template_id);                   //设置模版ID
		
		JSONObject jsonobj = new JSONObject();
		try {
			AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
			
			request.getSession().setAttribute("verification_code", verification_code);   //存入session，以便后台验证
			
			jsonobj.put("result", true);
			jsonobj.put("verification_code", verification_code);
			jsonobj.put("re", rsp.getBody());
			out.print(jsonobj.toString());
			out.close();
		} catch (ApiException e) {
			jsonobj.put("result", false);
			e.printStackTrace();
		}
		
		
	}

}
